﻿using Email.model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Email
{
    public partial class emailForm : Form
    {
        private MailManger mailManger;
        private int currentPage = 1;
        private int itemsPerPage = 5;
        private string username = "???";

        public emailForm(string username, string smtp)
        {
            InitializeComponent();
            this.label1.Text = username;
            this.username=username.Replace("@qq.com","qq");


            this.dataGridView.Columns.Add("From", "From");
            this.dataGridView.Columns.Add("To", "To");
            this.dataGridView.Columns.Add("Subject", "Subject");
            this.dataGridView.Columns.Add("Date", "Date");

            // 设置列的宽度
            this.dataGridView.Columns["From"].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
            this.dataGridView.Columns["To"].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
            this.dataGridView.Columns["Subject"].Width = 250;
            this.dataGridView.Columns["Date"].Width = 250;

            // 获取数据
            mailManger = new MailManger(username, smtp);
            mailManger.MessageHeadInit();

            // 为 this.dataGridView 设置 CellClick 事件
            this.dataGridView.CellDoubleClick += DataGridView_CellDoubleClick;

            // 显示第一页的数据
            ShowPage(currentPage);

        }
        public emailForm(): this("2411847932@qq.com", "lgifeikmuvmedhig"){ }
        private void ShowPage(int page)
        {
            // 计算开始和结束索引
            int startIndex = (page - 1) * itemsPerPage;
            int endIndex = Math.Min(startIndex + itemsPerPage, mailManger.messageHeads.Count);

            // 清除 DataGridView
            this.dataGridView.Rows.Clear();

            // 添加当前页的数据
            for (int i = startIndex; i < endIndex; i++)
            {
                this.dataGridView.Rows.Add(mailManger.messageHeads[i].From, mailManger.messageHeads[i].To, mailManger.messageHeads[i].Subject, mailManger.messageHeads[i].Date);
            }


            // 更新当前页标签
            lblCurrentPage.Text = $"{page}";

            // 更新总页数标签
            lblTotalPages.Text = $"{CalculateTotalPages()}";
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            int totalPages = (int)Math.Ceiling((double)mailManger.messageHeads.Count / itemsPerPage);
            if (currentPage < totalPages)
            {
                currentPage++;
                ShowPage(currentPage);
            }
        }

        private void btnPrevious_Click(object sender, EventArgs e)
        {
            if (currentPage > 1)
            {
                currentPage--;
                ShowPage(currentPage);
            }
        }

        private void DataGridView_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            // 检查点击的是哪一行
            if (e.RowIndex >= 0 && e.RowIndex < this.dataGridView.Rows.Count - 1)
            {
                // 获取点击的行
                DataGridViewRow row = this.dataGridView.Rows[e.RowIndex];

                // 获取当前页第一条记录的索引
                int firstRecordIndex = (currentPage - 1) * itemsPerPage;

                // 计算实际的 List<MessageInfo> 中的索引
                int actualIndex = firstRecordIndex + e.RowIndex;

                this.Hide();
                email_detail ed = new email_detail(actualIndex+1,this.username);
                ed.ShowDialog();
                this.Show();

            }
        }

        private void btn4delete_Click(object sender, EventArgs e)
        {
            // 获取选中的行
            if (dataGridView.SelectedRows.Count > 0)
            {
                DataGridViewRow selectedRow = this.dataGridView.CurrentRow;

                // 获取当前页第一条记录的索引
                int firstRecordIndex = (currentPage - 1) * itemsPerPage;

                // 计算实际的 List<MessageInfo> 中的索引
                int actualIndex = firstRecordIndex + selectedRow.Index;


                // 注意索引从0开始，但邮件序号从1开始
                mailManger.Delete(actualIndex + 1);

                mailManger.MessageHeadInit();

                // 重新显示数据
                currentPage = 1; // 重置当前页为第一页
                ShowPage(currentPage);

            }
            else
            {
                MessageBox.Show("Please select a row to delete.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }


        private int CalculateTotalPages()
        {
            return (int)Math.Ceiling((double)mailManger.messageHeads.Count / itemsPerPage);
        }
    }
}
